package x.trident.core.domain;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import x.trident.core.constants.Constant;
import x.trident.core.util.DateUtils;
import x.trident.core.util.NumberUtils;

import java.time.LocalDateTime;

/**
 * wuqiangfu special annotation
 *
 * @Package:        com.maas.platform.common.domain
 * @FileName:       BaseEntity.java
 * @ClassName:      BaseEntity
 * @Description:    基础实体类
 * @Author:         wuqiangfu
 * @CreateDate:     2022/6/10 13:41
 * @UpdateUser:     wuqiangfu
 * @UpdateDate:     2022/6/10 13:41
 * @UpdateRemark:   说明本次修改内容
 * @Version:        v1.0
 */
@Data
@EqualsAndHashCode(callSuper = true)
public abstract class BaseEntity<T extends Model<?>> extends Model<T> {

    /**
     *  id
     */
    @TableField(value = "id", fill = FieldFill.INSERT)
    private Long id;

    /**
     *  启用标识 0:启用 1:停用
     */
    @TableField(value = "enable_flag", fill = FieldFill.INSERT)
    private Integer enableFlag;

    /**
     *  删除标识 0:未删除 1:删除
     */
    @TableField(value = "delete_flag", fill = FieldFill.INSERT)
    private Integer deleteFlag;

    /**
     *  创建人id
     */
    @TableField(value = "create_user_id", fill = FieldFill.INSERT)
    private Long createUserId;

    /**
     *  创建人名字
     */
    @TableField(value = "create_user_name", fill = FieldFill.INSERT)
    private String createUserName;

    /**
     *  创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     *  修改人id
     */
    @TableField(value = "update_user_id", fill = FieldFill.INSERT_UPDATE)
    private Long updateUserId;

    /**
     *  修改人名字
     */
    @TableField(value = "update_user_name", fill = FieldFill.INSERT_UPDATE)
    private String updateUserName;

    /**
     *  修改时间
     */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    protected void assignmentData(Long createUserId, String createUserName) {
        this.createUserId = createUserId;
        this.createUserName = createUserName;
    }

    /**
     *  生成id
     *
     * @return 返回主键id
     */
    public Long generateId () {
        this.id = NumberUtils.generator();
        return id;
    }

    /**
     * wuqiangfu special annotation
     *
     * @return v
     * @Description:  创建
     */
    public void createData (Long createUserId, String createUserName) {
        LocalDateTime now = DateUtils.parseDateTime(DateUtils.formatDateTime(LocalDateTime.now()));
        this.deleteFlag = Constant.DELETE_FALSE;
        this.createUserId = createUserId;
        this.createUserName = createUserName;
        this.createTime = now;
        this.updateUserId = createUserId;
        this.updateUserName = createUserName;
        this.updateTime = now;
    }

    /**
     * wuqiangfu special annotation
     *
     * @return v
     * @Description:  修改
     */
    public void updateData (Long updateUserId, String updateUserName) {
        this.updateUserId = updateUserId;
        this.updateUserName = updateUserName;
        this.updateTime = DateUtils.parseDateTime(DateUtils.formatDateTime(LocalDateTime.now()));
    }

    /**
     * wuqiangfu special annotation
     *
     * @return v
     * @Description:  启用
     */
    public void startEnable () {
        this.enableFlag = Constant.ENABLE_TRUE;
    }

    /**
     * wuqiangfu special annotation
     *
     * @return v
     * @Description:  停用
     */
    public void closeEnable () {
        this.enableFlag = Constant.ENABLE_FALSE;
    }

    /**
     * wuqiangfu special annotation
     *
     * @return v
     * @Description:  删除(逻辑删除)
     */
    public void delete () {
        this.deleteFlag = Constant.DELETE_TRUE;
    }

}
